#include "cwit_list.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"


//初始化
cwit_list* cwit_list_init()
{
    cwit_list * list = (cwit_list *)malloc(sizeof(cwit_list));
	memset(list,0,sizeof(cwit_list));
    if(list == NULL)
        return NULL;
    list->tail = NULL;
    list->size = 0;
	list->header.front = NULL;
	list->header.next = NULL;
	list->header.data = NULL;
    return list;
}

//指定位置插入
void cwit_list_insert_position(cwit_list *list, int pos, void* data){
	if (list == NULL){
		return;
	}
	if (data == NULL){
		return;
	}

	cwit_list* mlist = list;
	//判断位置是否越界 位置从0开始,如果越界，默认插入到尾部
	if (pos < 0 || pos > mlist->size){
		pos = mlist->size;
	}

	//查找插入位置
	struct _CWIT_LIST_NODE* pCurrent = &(mlist->header);
	int i = 0;
	for (; i < pos;i++){
		pCurrent = pCurrent->next;
	}

	//创建新节点
	struct _CWIT_LIST_NODE* newnode = (struct _CWIT_LIST_NODE*)malloc(sizeof(struct _CWIT_LIST_NODE));
	newnode->data = data;
	newnode->next = NULL;
    newnode->front = NULL;

	//新节点入链表
	newnode->next = pCurrent->next;
    newnode->front = pCurrent;
	pCurrent->next = newnode;
	mlist->size++;
}

void cwit__list_push_header(cwit_list *list,void* data)
{
	cwit_list_insert_position(list,0,data);
}

void cwit_list_push_tail(cwit_list *list,void* data)
{
	cwit_list_insert_position(list,list->size,data);
}

//指定条件插入
void cwit_list_insert_condition(cwit_list *list,void *data,cwit_list_condition_fun condition_fun,void *userparam){
	if (list == NULL){
		return;
	}
	if (data == NULL){
		return;
	}

	cwit_list* mlist = list;
	//遍历链表
	struct _CWIT_LIST_NODE* pCurrent = &(mlist->header);
	for (int i = 0; i < mlist->size+1;i++){
		void *nextdat = pCurrent->next == NULL?NULL:pCurrent->next->data;
		if(condition_fun(pCurrent->data,data,nextdat,userparam))
		{
			//创建新节点
			struct _CWIT_LIST_NODE* newnode = (struct _CWIT_LIST_NODE*)malloc(sizeof(struct _CWIT_LIST_NODE));
			newnode->data = data;
			newnode->next = NULL;
			newnode->front = NULL;

			//新节点入链表
			newnode->next = pCurrent->next;
			newnode->front = pCurrent;
			pCurrent->next = newnode;
			mlist->size++;
			return ;
		}
		pCurrent = pCurrent->next;
	}
}

//移除指定位置元素
void* cwit_list_remove_position(cwit_list *list, int pos){
	if (list == NULL){
		return NULL;
	}
	cwit_list* mlist = (cwit_list*)list;
	if (pos < 0 || pos >= mlist->size){
		return NULL;
	}
	//判断链表是否为空
	if (mlist->size == 0){
		return NULL;
	}
	void* rtn_data = NULL;
	//查找删除节点的前一个节点
	struct _CWIT_LIST_NODE* pCurrent = &(mlist->header);
	int i = 0;
	for (; i < pos;i++){
		pCurrent = pCurrent->next;
	}
	//缓存待删除节点
	struct _CWIT_LIST_NODE* pDel = pCurrent->next;
	//重新连接被删除节点前驱和后继节点
	pCurrent->next = pDel->next;
	//释放被删除节点内存
	/*if(pDel->data)
		free(pDel->data);*/
	rtn_data = pDel->data;
	free(pDel);
	mlist->size--;
	return rtn_data;		/* 注意这里返回的数据指针是无效的 */
}

void *cwit_list_remove_header(cwit_list *list)
{
	return cwit_list_remove_position(list,0);
}

void *cwit_list_remove_tail(cwit_list *list)
{
	return cwit_list_remove_position(list,list->size-1);
}

//移除指定位置元素
void cwit_list_remove_condition(cwit_list *list, cwit_list_condition_fun condition_fun,void *userparam){
	if (list == NULL){
		return ;
	}
	cwit_list* mlist = (cwit_list*)list;

	//判断链表是否为空
	if (mlist->size == 0){
		return ;
	}
	//查找删除节点的前一个节点
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;
	while (pCurrent != NULL){
		void *front = pCurrent->front == NULL?NULL:pCurrent->front->data;
		void *nextdat = pCurrent->next == NULL?NULL:pCurrent->next->data;
		if(condition_fun(front,pCurrent->data,nextdat,userparam))
		{
			//缓存待删除节点
			struct _CWIT_LIST_NODE* pDel = pCurrent;
			//重新连接被删除节点前驱和后继节点
			if(pCurrent->front != NULL)
				pCurrent->front->next = pDel->next;
			if(pCurrent->next != NULL)
				pCurrent->next->front = pCurrent->front;
			pCurrent = pCurrent->next;
			//释放被删除节点内存
			/*if(pDel->data)
				free(pDel->data);*/
			free(pDel);
			mlist->size--;
		}
		else
		{
			pCurrent = pCurrent->next;
		}
	}

	return ;		/* 注意这里返回的数据指针是无效的 */
}

cwit_list *cwit_list_remove_condition_to_list(cwit_list *list, cwit_list_condition_fun condition_fun,void *userparam)
{
	
	if (list == NULL){
		return NULL;
	}
	cwit_list* mlist = (cwit_list*)list;

	//判断链表是否为空
	if (mlist->size == 0){
		return NULL;
	}
	cwit_list *newlist = malloc(sizeof(cwit_list));
	memset(newlist,0,sizeof(cwit_list));
	//查找删除节点的前一个节点
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;
	while (pCurrent != NULL){
		void *front = pCurrent->front == NULL?NULL:pCurrent->front->data;
		void *nextdat = pCurrent->next == NULL?NULL:pCurrent->next->data;
		if(condition_fun(front,pCurrent->data,nextdat,userparam))
		{
			cwit_list_push_tail(newlist,pCurrent->data);
			//缓存待删除节点
			struct _CWIT_LIST_NODE* pDel = pCurrent;
			//重新连接被删除节点前驱和后继节点
			if(pCurrent->front != NULL)
				pCurrent->front->next = pDel->next;
			if(pCurrent->next != NULL)
				pCurrent->next->front = pCurrent->front;
			pCurrent = pCurrent->next;
			//释放被删除节点内存
			/*if(pDel->data)
				free(pDel->data);*/
			free(pDel);
			mlist->size--;
		}
		else
		{
			pCurrent = pCurrent->next;
		}
	}

	return newlist;
}
//移除指定指定值元素
void cwit_list_remove_value(cwit_list *list,void *data,int datsize,int isfree){
	if (list == NULL){
		return ;
	}
	cwit_list* mlist = (cwit_list*)list;

	//判断链表是否为空
	if (mlist->size == 0){
		return ;
	}
	//查找删除节点的前一个节点
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;

	while (pCurrent != NULL){
		if(memcmp(pCurrent->data,data,datsize) == 0)
		{
			//缓存待删除节点
			struct _CWIT_LIST_NODE* pDel = pCurrent;
			//重新连接被删除节点前驱和后继节点
			if(pCurrent->front != NULL)
				pCurrent->front->next = pDel->next;
			if(pCurrent->next != NULL)
				pCurrent->next->front = pCurrent->front;
			pCurrent = pCurrent->next;
			//释放被删除节点内存
			if(pDel->data && isfree)
				free(pDel->data);
			free(pDel);
			mlist->size--;
		}
		else
		{
			pCurrent = pCurrent->next;
		}
	}

	return ;		/* 注意这里返回的数据指针是无效的 */
}

//链表大小
int cwit_list_size(cwit_list* list){
	if (list == NULL){
		return -1;
	}
	cwit_list* mlist = (cwit_list*)list;
	return mlist->size;
}

//获取指定位置元素
void* cwit_list_get_ops(cwit_list* list, int pos)
{
	cwit_list* mlist = (cwit_list*) list;

	if (list == NULL) {
		return NULL;
	}
	if (pos < 0 || pos > mlist->size){
		return NULL;
	}
	//查找pos前一个位置的节点
	struct _CWIT_LIST_NODE* pCurrent = &(mlist->header);
	int i = 0;
	for (i = 0; i < pos;i++){
		pCurrent = pCurrent->next;
	}

	/* BUG: 如果数据链表中没有元素呢？ */
	if (pCurrent->next == NULL) {
		return NULL;
	}
	return pCurrent->next->data;
}
void *cwit__list_get_header(cwit_list* list)
{
	return cwit_list_get_ops(list,0);
}
void *cwit_list_get_tail(cwit_list* list)
{
	return cwit_list_get_ops(list,list->size);
}

//移除指定位置元素
void *cwit_list_get_condition(cwit_list* list,cwit_list_condition_fun condition_fun,void *userparam){
	if (list == NULL){
		return NULL;
	}
	cwit_list* mlist = (cwit_list*)list;

	//判断链表是否为空
	if (mlist->size == 0){
		return NULL;
	}
	//查找删除节点的前一个节点
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;
	while (pCurrent != NULL){
		void *front = pCurrent->front == NULL?NULL:pCurrent->front->data;
		void *nextdat = pCurrent->next == NULL?NULL:pCurrent->next->data;
		if(condition_fun(front,pCurrent->data,nextdat,userparam))
		{
			return pCurrent->data;
		}
		else
		{
			pCurrent = pCurrent->next;
		}
	}
	return NULL;		/* 注意这里返回的数据指针是无效的 */
}

void cwit_list_traversal(cwit_list *list, cwit_list_condition_fun traversal_fun,void *userparam)
{
	if (list == NULL){
		return ;
	}
	cwit_list* mlist = (cwit_list*)list;

	//判断链表是否为空
	if (mlist->size == 0){
		return ;
	}
	//查找删除节点的前一个节点
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;
	while (pCurrent != NULL){
		void *front = pCurrent->front == NULL?NULL:pCurrent->front->data;
		void *nextdat = pCurrent->next == NULL?NULL:pCurrent->next->data;
		struct _CWIT_LIST_NODE *current = pCurrent;
		pCurrent = pCurrent->next;
		traversal_fun(front,current->data,nextdat,userparam);
	}
	return ;
}
//////////////////////////////////////待更新实现//////////////////////////////////////////////
//打印函数指针
typedef void(DATA_PRINT)(void*);
//遍历链表
void Print_LinkList(cwit_list* list, DATA_PRINT print){
	if (list == NULL){
		return;
	}
	cwit_list* mlist = (cwit_list*)list;
	//赋值指针
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;
	while (pCurrent!=NULL){
		print(pCurrent->data);
		pCurrent = pCurrent->next;
	}
}

//销毁链表
void Destroy_LinkList(cwit_list* list){
	if (list == NULL){
		return;
	}

	cwit_list* mlist = (cwit_list*)list;
	struct _CWIT_LIST_NODE* pCurrent = mlist->header.next;
	while (pCurrent != NULL){
		//缓存待删除节点下一个节点
		struct _CWIT_LIST_NODE* pNext = pCurrent->next;
		/*if(pCurrent->data)
			free(pCurrent->data);*/
		free(pCurrent);
		pCurrent = pNext;
	}
	//最后释放链表内存
	free(mlist);
}

//比较函数指针
typedef int(DATA_COMPARE)(void*, void*);

/* 获得指定位置元素
 * 在迭代时，线程不安全，寻求类似iterator迭代器的方式，对整个链表进行迭代
 */
void* cwit_list_get_opsByVal(cwit_list *list, void* data,DATA_COMPARE compare){

	if (list == NULL || data == NULL || compare == NULL){
			return NULL;
	}
	cwit_list* mlist = (cwit_list*) list;
	if (mlist->size == 0) {
		return NULL;
	}
	void* rtn_data = NULL;
	//查找删除节点
	struct _CWIT_LIST_NODE* pPrev = &(mlist->header);
	struct _CWIT_LIST_NODE* pCurrent = pPrev->next;
	while (pCurrent != NULL) {
		if (compare(pCurrent->data, data)) {
			rtn_data = pCurrent->data;
			break;
		}
		pPrev = pCurrent;
		pCurrent = pCurrent->next;
	}
	return rtn_data;
}
